#include <stdio.h>
#include <stdlib.h>
#include "minbinheap.h"

void min_heapify(PMinHeap pq, int i){
    if(left(i) >= pq -> heap_size - 1) {
        if(left(i) >= pq -> heap_size) return;
        if(pq -> heap_array[left(i)].value < pq -> heap_array[i].value)
            swap_node(pq -> heap_array + i, pq -> heap_array + left(i)),
            min_heapify(pq, left(i));
    } else {
        int isLeft = pq -> heap_array[left(i)].value < pq -> heap_array[right(i)].value;
        if(pq -> heap_array[i].value < pq -> heap_array[isLeft ? left(i) : right(i)].value) return;
        swap_node(pq -> heap_array + i, pq -> heap_array + (isLeft ? left(i) : right(i)));
        min_heapify(pq, isLeft ? left(i) : right(i));
    }
}
